package tjs.study.notes.mysql.mybatis.srevice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tjs.study.notes.mysql.mybatis.mapper.CmdbTempInstanceLevelMapper;

/**
 * Created by Nicky.Tang on 2022/7/2 下午7:38
 *
 * @since 02.12.10
 */
@Service
public class InsertService {

    @Autowired
    private CmdbTempInstanceLevelMapper cmdbTempInstanceLevelMapper;
    @Autowired
    private InsertService insertService;

    @Transactional
    public void doTest(int i) {
        //cmdbTempInstanceLevelMapper.copyIgnore(99l);
        insertService.doTestRC(i);
        // 后面的逻辑失败了，导致回滚，已提交的事务不会再回滚了
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)// 都已提交，将不会报错
    public void doTestRC(int i) {
        cmdbTempInstanceLevelMapper.copyIgnore(Long.valueOf(i));

        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //cmdbTempInstanceLevelMapper.copyIgnore(ThreadLocalRandom.current().nextLong(10));
    }
}
